home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v10n22.arc / ATOI.ASM next >
Assembly Source File  |  1991-12-08  |  3KB  |  81 lines

  1.         title   ATOI - ASCII to integer
  2.         page    55,132
  3.  
  4. ; ATOI.ASM --- Convert ASCII string to 16-bit decimal integer.
  5. ;
  6. ; Copyright (C) 1989 Ray Duncan
  7. ; Call with:    DS:SI = address of string,
  8. ;               where 'string' is in the form
  9. ;                  [whitespace][sign][digits]
  10. ;
  11. ; Returns:      AX    = result
  12. ;               DS:SI = address+1 of terminator 
  13. ;
  14. ; Destroys:     Nothing
  15. ;
  16. ; Like the C runtime library function 'atoi', this routine gives no 
  17. ; warning of overflow, and terminates on the first invalid character.
  18.  
  19. blank   equ     20h             ; ASCII blank character
  20. tab     equ     09h             ; ASCII tab character
  21.  
  22. _TEXT   segment word public 'CODE'
  23.  
  24.         assume  cs:_TEXT
  25.  
  26.         public  atoi
  27. atoi    proc    near
  28.  
  29.         push    bx                      ; save registers
  30.         push    cx
  31.         push    dx
  32.  
  33.         xor     bx,bx                   ; initialize forming answer
  34.         xor     cx,cx                   ; initialize sign flag
  35.  
  36. atoi1:  lodsb                           ; scan off whitespace
  37.         cmp     al,blank                ; ignore leading blanks
  38.         je      atoi1
  39.         cmp     al,tab                  ; ignore leading tabs
  40.         je      atoi1
  41.  
  42.         cmp     al,'+'                  ; if + sign, proceed
  43.         je      atoi2
  44.         cmp     al,'-'                  ; is it - sign?
  45.         jne     atoi3                   ; no, test if numeric
  46.         dec     cx                      ; was - sign, set flag
  47.                                         ; for negative result
  48.  
  49. atoi2:  lodsb                           ; get next character
  50.  
  51. atoi3:  cmp     al,'0'                  ; is character valid?
  52.         jb      atoi4                   ; jump if not '0' to '9'
  53.         cmp     al,'9'
  54.         ja      atoi4                   ; jump if not '0' to '9'
  55.  
  56.         and     ax,0fh                  ; isolate lower four bits
  57.  
  58.         xchg    bx,ax                   ; previous answer x 10
  59.         mov     dx,10
  60.         mul     dx
  61.  
  62.         add     bx,ax                   ; add this digit
  63.  
  64.         jmp     atoi2                   ; convert next digit
  65.  
  66. atoi4:  mov     ax,bx                   ; put result into AX
  67.         jcxz    atoi5                   ; jump if sign flag clear
  68.         neg     ax                      ; make result negative
  69.  
  70. atoi5:  pop     dx                      ; restore registers
  71.         pop     cx
  72.         pop     bx
  73.         ret                             ; back to caller
  74.  
  75. atoi    endp
  76.  
  77. _TEXT   ends
  78.  
  79.         end
  80.